package com.fishery.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fishery.authority.entity.BaseRoleFunction;
import com.fishery.authority.entity.Function;
import com.fishery.authority.mapper.BaseRoleFunctionMapper;
import com.fishery.authority.service.BaseRoleFunctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.entity.GlobalException;
import com.fishery.entity.Result;
import com.fishery.entity.StatusCode;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hy
 * @since 2021-01-18
 */
@Service
public class BaseRoleFunctionServiceImpl extends ServiceImpl<BaseRoleFunctionMapper, BaseRoleFunction> implements BaseRoleFunctionService {

    /**
     * 通过baseId和baseIdentity查functionId
     *
     * @param baseId
     * @param baseIdentity
     * @return
     */
    @Override
    public List<BaseRoleFunction> findFunctionByBaseIdBaseIdentity(String baseId, Integer baseIdentity) {
//        List<Function> functionList = baseMapper.findByBaseIdBaseIdentity(baseId, baseIdentity);
//        return functionList;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("base_id", baseId);
        wrapper.eq("base_identity", baseIdentity);
        List<BaseRoleFunction> baseRoleFunctions = baseMapper.selectList(wrapper);
        return baseRoleFunctions;
    }

    @Override
    public void deleteALLBaseFunctionById(String baseId, Integer baseIdentity) {
        QueryWrapper<BaseRoleFunction> wrapper = new QueryWrapper<>();
        wrapper.eq("base_id", baseId);
        wrapper.eq("base_identity", baseIdentity);
        int result = baseMapper.delete(wrapper);
    }

    /**
     * 基地老板给基地用户授权
     *
     * @param baseId
     * @param baseIdentity
     * @param functionId
     */
    @Override
    public void addBaseRoleFunction(String baseId, Integer baseIdentity, String functionId) {
        if(StringUtils.isEmpty(baseId) || StringUtils.isEmpty(baseIdentity) || StringUtils.isEmpty(functionId)) {
            throw new GlobalException((new Result(false, StatusCode.ERROR, "请填写所有信息", null)));
        }
        BaseRoleFunction baseRoleFunction = new BaseRoleFunction();
        baseRoleFunction.setBaseId(baseId);
        baseRoleFunction.setBaseIdentity(baseIdentity);
        baseRoleFunction.setFunctionId(functionId);
        baseMapper.insert(baseRoleFunction);
    }
}
